﻿/// 开发者：陆乘胜
/// 2018.10.18
/// luchengsheng@wuling.com
/// 17376394205
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using WebApplication2.Model;
using WebApplication2.Server;
using WebApplication2.Util;

namespace WebApplication2.Handler
{
    /// <summary>
    /// projectHandler 的摘要说明
    /// 与project相关的一般处理程序，包括
    /// 1.新建项目
    /// 2.查询全部项目
    /// </summary>
    public class projectHandler : IHttpHandler,IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string funC = context.Request["func"];
            try
            {
                switch (funC)
                {
                    case "addProject":
                        addProject(context);
                        break;
                    case "queryAllProject":
                        queryAllProject(context);
                        break;
                    case "queryInCehua":
                        queryInCehua(context);
                        break;
                    case "queryInDesign":
                        queryInDesign(context);
                        break;
                    case "queryInWorking":
                        queryInWorking(context);
                        break;
                    case "queryInDebuging":
                        queryInDebugging(context);
                        break;
                    case "queryFinished":
                        queryFinished(context);
                        break;
                    case "inSuspend":
                        queryzanting(context);
                        break;
                    case "searchByID":
                        searchByID(context);
                        break;
                    case "searchByName":
                        searchByName(context);
                        break;
                    case "searchByChargeMan":
                        searchByChargeMan(context);
                        break;
                    case "delete":
                        DeleteProject(context);
                        break;

                    case "updateProject":
                        UpdateProjectStatus(context);
                        break;
                  
                    default:
                        context.Response.Write("{data:\"参数错误！\"}");
                        Console.WriteLine("傻逼,参数错误！");
                        break;
                }
            }
            catch (Exception e)
            {
                context.Response.ClearContent();
                context.Response.Write(e.ToString());
            }
        }

        /// <summary>
        /// 处于暂停状态的项目
        /// </summary>
        /// <param name="context"></param>
        private void inSuspend(HttpContext context)
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(6);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();
            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "在设计阶段的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);

        }

        /// <summary>
        /// 添加新的项目
        /// code返回1成功，0失败
        /// </summary>
        /// <returns></returns>
        public void addProject(HttpContext context)
        {
            FeedBackMsg<Dynamics> feedBack = new FeedBackMsg<Dynamics>();
            int code = 0;
            string msg = "";
            string json = "";
            //执行数据库的插入操作
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.Id = context.Request["pro_id"];
            projectInfo.NewID= context.Request["NewID"];
            projectInfo.Name = context.Request["name"];
            projectInfo.UserArea = context.Request["area"];
            projectInfo.Budget = int.Parse(context.Request["budget"]);
            //projectInfo.RealBudge = int.Parse(context.Request["realBudget"]);
            projectInfo.Value = int.Parse(context.Request["value"]);
            projectInfo.InputTime = Convert.ToDateTime(context.Request["inputTime"]);
            projectInfo.FinishTime = Convert.ToDateTime(context.Request["finishTime"]);
            projectInfo.MCharge = context.Request["m_charge"];
            projectInfo.MDesign = context.Request["m_design"];
            projectInfo.Echarge = context.Request["e_charge"];
            projectInfo.EDesign = context.Request["e_design"];
            projectInfo.ChargeMan = context.Request["chargeMan"];
            projectInfo.Caps = int.Parse(context.Request["caps"]);
            projectInfo.ProType = int.Parse(context.Request["protype"]);

            projectInfo.Status = 1;

            string pro_id = context.Request["pro_id"];
            string sql1 = "select * from T_ProjectInfo where F_ID= '" + pro_id + "'";
            DataRowCollection rows = MrDBAccess.ExecuteDataSet(sql1).Tables[0].Rows;

            if (rows.Count == 0)
            {

                int res = ProjectWebService.Instance.AddNewProject(projectInfo);
                if (res == 0)
                {
                    code = 1;
                    msg = "添加失败";
                    feedBack.Code = code;
                    feedBack.Msg = msg;
                    json = ObjToJson.ToJson(feedBack);
                    context.Response.Write(json);
                    return;
                }
                else
                {
                    code = 1;
                    msg = "添加成功";
                    feedBack.Code = code;
                    feedBack.Msg = msg;
                    json = ObjToJson.ToJson(feedBack);
                    context.Response.Write(json);
                    return;
                }
            }
            else json = "{\"Code\":\"" + 2 + "\",\"Msg\":\"" + msg + "\"}";
            context.Response.Write(json);
        }


        /// <summary>
        /// 更新项目进度
        /// </summary>
        public void UpdateProjectStatus(HttpContext context) {

            int code = 0;
            string msg = "";
            string json = "";
            string setStatus = context.Request["setstatus"];
            string status = context.Request["status"];
            string oprator = context.Request["oprator"];
            string id =context.Request["id"];
            int res = ProjectWebService.Instance.UpdateStatus(setStatus, id);
            if (res == 1) {
                msg = "";
                code = 1;
            }
            else {
                msg = "删除失败";
                code = 0;
            }
            json = "{\"Code\":\"" + code + "\",\"Msg\":\"" + msg + "\"}";
            context.Response.Write(json);

        }


        public void DeleteProject(HttpContext context)
        {
            int code = 0;
            string msg = "";
            string json = "";
            string name = context.Request["name"];
            string id = context.Request["id"];

            int res = ProjectWebService.Instance.DeletePro(null,id);
            if (res == 0) { msg = "删除失败"; }
            else { code = 1;msg = "删除成功"; }
            json = "{\"Code\":\"" + code + "\",\"Msg\":\"" + msg + "\"}";
            context.Response.Write(json);

        }


        /// <summary>
        /// 查询全部项目
        /// 返回一个带有全部项目内容的Json
        /// </summary>
        /// <returns></returns>
        public void queryAllProject(HttpContext context) 
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo= ProjectWebService.Instance.GetProjectList(0);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<ProjectInfo>>();
            if (ProjectWebService.Instance != null) 
            {
                feedBack.Code = 1;
                feedBack.Msg = "所有的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }
        /// <summary>
        /// 策划
        /// </summary>
        /// <param name="context"></param>
        public void queryInCehua(HttpContext context)
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(1);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();
            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "在策划阶段的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }

        /// <summary>
        /// 设计中的项目
        /// </summary>
        public void queryInDesign(HttpContext context) 
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(2);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();
            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "在设计阶段的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }

        /// <summary>
        /// 加工中的项目
        /// </summary>
        /// <param name="context"></param>
        public void queryInWorking(HttpContext context) 
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(3);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();


            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "在加工状态的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);


        }
        /// <summary>
        /// 调试中的项目
        /// </summary>
        public void queryInDebugging(HttpContext context)
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(4);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();
            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "调试装备的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);

        }
        /// <summary>
        /// 已完成的项目
        /// </summary>
        /// <param name="context"></param>
        public void queryFinished(HttpContext context) 
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(5);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();


            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "已经完成的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }


        /// <summary>
        /// 暂停的项目
        /// </summary>
        /// <param name="context"></param>
        public void queryzanting(HttpContext context)
        {
            string json = "";
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(6);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();


            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "已经完成的项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }

        /// <summary>
        /// ID搜索
        /// </summary>
        /// <param name="context"></param>
        public void searchByID(HttpContext context) 
        {
            string json = "";
            string ID = context.Request["id"];
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.GetProjectList(1);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();


            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "用户通过搜索ID项目信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }



        /// <summary>
        /// 通过项目名搜索
        /// </summary>
        /// <param name="context"></param>
        public void searchByName(HttpContext context)
        {
            string json = "";
            //获取前端页面发送过来的项目名Name
            string name = context.Request["name"];
            //string name = context.Request["Obj"];
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.SearchServiceByName(name);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();
            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "用户通过搜索项目名获取的信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }

        /// <summary>
        /// 通过负责人搜索
        /// </summary>
        /// <param name="context"></param>
        public void searchByChargeMan(HttpContext context)
        {
            string json = "";

            string chargeName = context.Request["chargeName"];
            //查询返回多条结果，采用泛型集合保存查询结果
            List<ProjectInfo> ProjectInfo = ProjectWebService.Instance.SearchServiceByChargeMan(chargeName);
            //一个泛型集合的feedBack
            FeedBackMsg<List<ProjectInfo>> feedBack = new FeedBackMsg<List<Model.ProjectInfo>>();
            if (ProjectWebService.Instance != null)
            {
                feedBack.Code = 1;
                feedBack.Msg = "用户通过搜索负责人名信息";
                feedBack.Obj = ProjectInfo;
            }
            json = ObjToJson.ToJson(feedBack);
            context.Response.Write(json);
        }




        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}